package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Role;
import com.itheima.pojo.Menu;
import com.itheima.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {

	@Autowired
	UserDao userDao;

	@Override
	public boolean login(String username, String password) {
		log.debug("service_provide...u:" + username + " p:" + password);
		if ("admin".equals(username) && "123".equals(password)) {
			return true;
		} else {
			throw new BusinessRuntimeException("登录失败!!!");
		}
	}

    @Autowired
    private MenuDao menuDao;


	@Override
	public SysUser findByUsername(String username) {
		return userDao.findByUsername(username);
	}

	@Override
	@Transactional
	public void add(SysUser sysUser, Integer[] roleIds) {
		//1. 添加检查组对象， 回显主键id
		userDao.add(sysUser);
		//2. 维护中间表的数据（检查组和检查项的关系）
		if (sysUser.getId() != null) {
			setRelation(sysUser.getId(), roleIds);
		}
	}

	public void setRelation(Integer sysUserId ,Integer[] roleId){
		if(sysUserId != null && roleId != null && roleId.length > 0){
			for (Integer rolesId : roleId) {
				userDao.set(sysUserId ,rolesId );
			}
		}
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		Page<SysUser> page = userDao.findByCondition(queryPageBean.getQueryString());
		return new PageResult(page.getTotal(), page);
	}

	@Override
	public SysUser findById(Integer id) {
		return userDao.findById(id);
	}

	@Override
	public void delById(Integer id) {
		long count = userDao.findSetmealCountById(id);
		if (count > 0) {
			throw new BusinessRuntimeException("该用户数据被角色关联，不能删除！！！");
		}
		userDao.delRelation(id);

		userDao.delById(id);
	}



	@Override
	public List<Integer> findroleIdsById(Integer id) {
		List<Integer> list=userDao.findroleIdsById(id);
		log.debug(list.toString());
		return list;
	}

	@Override
	@Transactional
	public void edit(SysUser sysUser, Integer[] roleIds) {
		//1.修改用户数据
		userDao.edit(sysUser);
		//2.1 先删除该用户数据原来的关系
		userDao.delRelation(sysUser.getId());
		//2.2 添加新的关系
		if (sysUser.getId() != null && roleIds != null && roleIds.length > 0) {
			setRelation(sysUser.getId(), roleIds);
		}
	}

	public List<Role> findAll() {
		return userDao.findAll();
	}

    @Override
    public List<Map<String, Object>> getShowMenu(String username) {
        List<Menu> firstList = menuDao.findFirstMenu(username, 1);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Menu menu : firstList) {
            Map<String, Object> firstMap = new HashMap<>();
            firstMap.put("path", menu.getPath());
            firstMap.put("title", menu.getName());
            firstMap.put("icon", menu.getIcon());
            List<Menu> children = menuDao.getSecondMenu(username, 2, menu.getId());
            List<Map<String, Object>> list_2 = new ArrayList<>();
            for (Menu child : children) {
                Map<String, Object> secondMap = new HashMap<>();
                secondMap.put("path", child.getPath());
                secondMap.put("title", child.getName());
                secondMap.put("linkUrl", child.getLinkUrl());
                secondMap.put("children",child.getChildren());
                list_2.add(secondMap);
            }
            firstMap.put("children", list_2);
            list.add(firstMap);
        }
        return list;
    }
}